import axios from 'axios'

const Urls = {
  getWebVersion: 'http://dj.smk17.cn/chcp.json',
  getNewsList : 'http://dingtalk.smk17.cn/getNewsList.json'
}

/**
 * 通过后端get数据
 * @param url URL
 * @param params 参数，可选
 */
function get (url: string, params = {}) {
  return new Promise((resolve, rejct) => {
    axios.get(url)
    .then(response => {
      resolve(response.data)
    }, error => {
      rejct(error)
    })
  })
}

/**
 * 通过后端post数据
 * @param url URL
 * @param data 数据
 */
function post (url: string, data = {}) {
  return new Promise((resolve, rejct) => {
    axios.post(url)
    .then(response => {
      resolve(response.data)
    }, error => {
      rejct(error)
    })
  })
}

/**
 * 通过后端patch数据
 * @param url URL
 * @param data 数据
 */
function patch (url: string, data = {}) {
  return new Promise((resolve, rejct) => {
    axios.patch(url)
    .then(response => {
      resolve(response.data)
    }, error => {
      rejct(error)
    })
  })
}

/**
 * 通过后端put数据
 * @param url URL
 * @param data 数据
 */
function put (url: string, data = {}) {
  return new Promise((resolve, rejct) => {
    axios.put(url)
    .then(response => {
      resolve(response.data)
    }, error => {
      rejct(error)
    })
  })
}

export default {
  Urls,
  get,
  post,
  patch,
  put
}
